package com.mingrisoft;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.mingrisoft.database.DBOpenHelper;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class registerActivity extends Activity implements View.OnClickListener {
    EditText editAgainAccout, registerPasswordEditText, againPasswordEditText;
    private DBOpenHelper dbOpenHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        editAgainAccout = (EditText) findViewById(R.id.registerAccountEditText);
        registerPasswordEditText = (EditText) findViewById(R.id.registerPasswordEditText);
        againPasswordEditText = (EditText) findViewById(R.id.againPasswordEditText);
        Button btnNowRegister = (Button) findViewById(R.id.nowRegisterButton);
        Button btnBackLogin = (Button) findViewById(R.id.backLoginButton);
        editAgainAccout.setOnFocusChangeListener(new MyFocusChangeLisenter());
        editAgainAccout.addTextChangedListener(new HideTextWatcher(editAgainAccout, 11));
        registerPasswordEditText.setOnFocusChangeListener(new MyFocusChangeLisenter());
        registerPasswordEditText.addTextChangedListener(new HideTextWatcher(registerPasswordEditText, 6));
        againPasswordEditText.setOnFocusChangeListener(new MyFocusChangeLisenter());
        againPasswordEditText.addTextChangedListener(new HideTextWatcher(againPasswordEditText, 6));
        btnNowRegister.setOnClickListener(this);
        btnBackLogin.setOnClickListener(this);
        dbOpenHelper=new DBOpenHelper(registerActivity.this,"user.db",null,1);
    }
    protected void onStart() {
        super.onStart();
    }

    protected void onDestroy() {
        super.onDestroy();
        if (dbOpenHelper != null) {
            dbOpenHelper.close();
        }
    }


    public void onClick(View view) {
        String userAccount=editAgainAccout.getText().toString();
        String userPassword=registerPasswordEditText.getText().toString();
        String comfirmPassword=againPasswordEditText.getText().toString();
        Intent intent =new Intent();
        switch (view.getId()) {
            case R.id.nowRegisterButton:
                //判断异常情况弹窗
                //编辑框为空
                if(TextUtils.isEmpty(userAccount)){
                    Toast.makeText(registerActivity.this,"用户名不能为空！",Toast.LENGTH_SHORT).show();
                    //对用户名进行手机号正则化验证，调用下面写的idTelPhoneNumber方法
                }else if(!isTelPhoneNumber(userAccount)){
                    Toast.makeText(registerActivity.this,"请输入正确的手机号码！",Toast.LENGTH_SHORT).show();
                } else if(TextUtils.isEmpty(userPassword)){
                    Toast.makeText(registerActivity.this,"密码不能为空！",Toast.LENGTH_SHORT).show();
                    //两次密码框内容不一致
                }else if(!TextUtils.equals(userPassword,comfirmPassword)){
                    Toast.makeText(registerActivity.this,"密码不一致！",Toast.LENGTH_SHORT).show();
                } else{
                    //存储注册的用户名和密码 把账号密码存储进数据库
                    insertData(dbOpenHelper.getReadableDatabase(),userAccount,comfirmPassword);
                    Toast.makeText(registerActivity.this,"注册成功！",Toast.LENGTH_SHORT).show();
                }

               break;
            case R.id.backLoginButton:
              //把当前信息打包返回登录页面
                Bundle bundle=new Bundle();
                bundle.putString("userAccount",userAccount);
                bundle.putString("userPassword",userPassword);
                intent.putExtras(bundle);
                registerActivity.this.setResult(2,intent);
                registerActivity.this.finish();
                break;
            default:
                break;
        }
    }
    //创建数据库的insert方法 插入数据方法
    private void insertData(SQLiteDatabase readableDatabase, String username1, String password1){
        ContentValues values=new ContentValues();
        values.put("username",username1);
        values.put("password",password1);
        readableDatabase.insert("user",null,values);
    }
    /*正则化验证手机号码方法*/
    public static boolean isTelPhoneNumber(String mobile) {
        if (mobile != null && mobile.length() == 11) {
            Pattern pattern = Pattern.compile("^1[3|4|5|6|7|8|9][0-9]\\d{8}$");
            Matcher matcher = pattern.matcher(mobile);
            return matcher.matches();
        }else{
            return false;
        }
    }

    class MyFocusChangeLisenter implements View.OnFocusChangeListener {
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                switch (v.getId()) {
                    case R.id.registerAccountEditText:
                        String userName = editAgainAccout.getText().toString();
                        if (TextUtils.isEmpty(userName) || userName.length() < 11) {
                            editAgainAccout.requestFocus();
                            Toast.makeText(registerActivity.this, "请输入11位账户名", Toast.LENGTH_SHORT).show();
                        }
                        break;
                    case R.id.registerPasswordEditText:
                        String userPassword =  registerPasswordEditText.getText().toString();
                        if (TextUtils.isEmpty(userPassword) || userPassword.length() < 6) {
                            registerPasswordEditText.requestFocus();
                            Toast.makeText(registerActivity.this, "请输入6位密码", Toast.LENGTH_SHORT).show();
                        }
                        break;
                    case R.id.againPasswordEditText :
                        String userAgainPassword =  againPasswordEditText.getText().toString();
                        if (TextUtils.isEmpty(userAgainPassword ) || userAgainPassword .length() < 6) {
                            againPasswordEditText.requestFocus();
                            Toast.makeText(registerActivity.this, "请输入6位密码", Toast.LENGTH_SHORT).show();
                        }
                        else
                        {
                            if(!userAgainPassword.equals(registerPasswordEditText.getText().toString()))
                            {
                                Toast.makeText(registerActivity.this, "两次密码不一致，请重新输入", Toast.LENGTH_SHORT).show();
                            }
                        }
                    break;
                    default:
                        break;
                }
            }
        }
    }
    private class HideTextWatcher implements TextWatcher {
        private EditText meditText;
        int maxLength;

        public HideTextWatcher(EditText editObject, int maxLength) {
            this.meditText = editObject;
            this.maxLength = maxLength;
        }

        public void beforeTextChanged(CharSequence s, int start,
                                      int count, int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        public void afterTextChanged(Editable s) {
            String str = s.toString();
            if (str.length() == maxLength) {
                //隐藏输入法
                ViewUtil.hideOneInputMethod(registerActivity.this, meditText);
            }
        }
    }
}
